/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */

/****************************************************************
 *  @file    isp_test.c
 *  @brief   ISP testcase source file
 *  @version v1.0
 *  @date    03. Apr. 2023
 ****************************************************************/
#include <string.h>
#include "printf.h"
#include "hal_api.h"

//#include "core_portme.h"



//int32_t test_coremark(void)
//{
//	printf("\r\n[CoreMark]: TEST START\r\n");
//	
//	coremark_main();
//
//	printf("[CoreMark]: TEST END\r\n");
//	return 0;
//}

/***************************************************************/
#include "dw_uart.h"
#include "liteModbus.h"
#include "LiteModbus_HoldReg.h"

#if 1
#include "FreeRTOS.h"
#include "event_groups.h"
static EventGroupHandle_t  UART_CompleteGroup = NULL;
enum{
  UART0_COMPLETE_BIT  =  0x01,
  UART1_COMPLETE_BIT  =  0x02,
  UART2_COMPLETE_BIT  =  0x04,
  UART3_COMPLETE_BIT  =  0x08,
  UART4_COMPLETE_BIT  =  0x10, 
};
#else
bool     ModbusFlag;  //Modbus½ÓÊÕÍêÖ¡±ê×¼Î»
#endif

char     RxBuffer[64];  //´®¿Ú½ÓÊÕ»º´æ
uint16_t RxLenght;      //´®¿Ú½ÓÊÕÖ¡³¤¶È
uint16_t countdown=0;   //Modbus¶ÏÖ¡µ¹¼ÆÊ±


/**
 * @brief  TIM3ÖÐ¶Ï´¦Àí»Øµ÷ ¸øFreeMODBUS¶ÏÖ¡ÓÃµÄ
 * @param  
 * @retval 
 */
void TIM50us_IRQHandler_CallBack(void* ptr)
{
	portBASE_TYPE taskWoken;
	
  if(countdown)     {countdown--;}  //Modbus¶ÏÖ¡µ¹¼ÆÊ±
  if(countdown==1)
  {
#ifdef EVENT_GROUPS_H
	xEventGroupSetBitsFromISR(UART_CompleteGroup, UART3_COMPLETE_BIT, &taskWoken);
#else
    ModbusFlag = true;  //Modbus½ÓÊÕÍêÖ¡±ê×¼Î»
#endif
  }
}
/**
 * @brief  USART1¿ÕÏÐÖÐ¶Ï´¦Àí»Øµ÷
 * @param  
 * @retval 
 */
void HAL_UART_RxCallback(void* ptr)
{
	RxBuffer[RxLenght++] = Hal_Uart_GetChar(UART_ID_3);;
	
    countdown = Modbus_Ctdw_Calculate(115200);  
}

extern QueueHandle_t RTC_Queue;

void prvTask1( void *pvParameters )
{
	writel(0x1, MEM_MAP_UART3_BASE_ADDR + 4);
	irq_attach(UART3_IRQn, HAL_UART_RxCallback, NULL);
	irq_enable(UART3_IRQn);
	irq_set_priority(UART3_IRQn, 6);
	
	Hal_Timer_StartPeriod(TIMER_ID_4, 50, TIM50us_IRQHandler_CallBack);
	irq_set_priority(TIMER4_IRQn, 15);

	printf("Modbus initial finish\n");
#ifdef EVENT_GROUPS_H
	EventBits_t uxBits;
	UART_CompleteGroup = Hal_Eventgrp_Create();
#endif

  while (1)
  {
#ifdef EVENT_GROUPS_H
	  /* Wait a maximum of £¿ms for either bit 0 or bit 3 to be set within  the event group.  Clear the bits before exiting. */
    uxBits = xEventGroupWaitBits(
                 UART_CompleteGroup,   /* The event group being tested. */
                 UART3_COMPLETE_BIT , /* The bits within the event group to wait for. */
                 pdTRUE,      /* BIT_0 & BIT_3 should be cleared before returning. */
                 pdFALSE,     /* Don't wait for both bits, either bit will do. */
                 0xFFFFFFFF );/* Wait a maximum of ?ms for either bit to be set. */
#else
    while(ModbusFlag==false)   //等待modbus接收标志位
    {
//      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, (GPIO_PinState)!ucRegCoilBuf[0]);  //ÏßÈ¦¼Ä´æÆ÷0x0000¿ØÖÆLED
      vTaskDelay(2);
    }
#endif
    //Modbus接收帧解析
    if(true == API_ModbusRTU_Slave_Analy(RxBuffer, &RxLenght))
    { //MODBUS解析正确
      Hal_Uart_PutStr(UART_ID_3, RxBuffer, RxLenght);  //串口应答帧
    }
	
	if(0 != (usRegHoldBuf[0]|usRegHoldBuf[1]|usRegHoldBuf[2]|usRegHoldBuf[3]|usRegHoldBuf[4]|usRegHoldBuf[5]))
	{ 
//		xQueueSend(RTC_Queue, &usRegHoldBuf, 0); 
		memset(usRegHoldBuf, 0, 6*2);
	}
	
    //初始化接收指针
    RxLenght = 0;
    
#ifndef EVENT_GROUPS_H
    //清modbus接收标志位
    ModbusFlag = false;
#endif
  }
}
